//http://fitpro.sourceforge.net/eclipse_install.html
package com.fit;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.source.Customer;
import com.source.CustomerDAO;
import com.source.SerializationUtil;

public class InsertCustomerDetails extends fit.ColumnFixture {

	public String name;
	public int age;
	public String residentialArea;
	public String phone;
	public int rating;

	public boolean addCustomer() {	
		Customer addedCustomer;

		CustomerDAO customerDAO = new CustomerDAO(this.getConnection());

		addedCustomer = new Customer();
		addedCustomer.setId(customerDAO.selectMaxId()+1);

		if(age<18 || age>120){
			return false;
		}else{
			addedCustomer.setAge(age);
		}

		if("".equals(residentialArea)){
			return false;
		}else if(!residentialArea.equalsIgnoreCase("North") && !residentialArea.equalsIgnoreCase("Center") && 
				!residentialArea.equalsIgnoreCase("South") && !residentialArea.equalsIgnoreCase("Sharon") && 
				!residentialArea.equalsIgnoreCase("Jerusalem")){
			return false;
		}else{
			addedCustomer.setResidentialArea(residentialArea);
		}

		for(char c : phone.toCharArray()){
			if(!Character.isDigit(c)){
				return false;  
			} else{
				continue;             
			}
		}
		addedCustomer.setPhone(phone);


		if(rating<1 || rating>10){
			return false;
		}else{
			addedCustomer.setRating(rating);
		}

		if(name==null || "".equals(name)){
			return false;
		}else{
			addedCustomer.setName(name);
		}

		/*	<!--name -  check 1)Valid 2)if String empty -->

		<!--residentialArea -  check 1)Valid(one of five) 2)if String empty 3)not from list -->

		<!-- phone - if number -->*/


		try {
			SerializationUtil.serialize(addedCustomer, "c:\\customer.out");
		} catch (IOException e) {
			e.printStackTrace();
		}

		customerDAO.insert();
		return true;
	}

	public Connection getConnection(){
		String url = "jdbc:mysql://"+ "localhost" +"/" + "EmployeePortalDb";
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(url, "root", "Braude");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return conn;
	}
}
